SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


ALTER   trigger [tb_comexpproportionexpense_ai] on dbo.tb_comexpproportionexpense for insert
as
  declare @nbid int,
          @nrelationbid int,
          @nbstate int,
          @ndcamount decimal(18,2), 
	  @namount decimal(18,2)
begin
  set nocount on
  declare tb_comexpproportionexpense_cur cursor local forward_only static optimistic for
  select bid,relationbid,dcamount,amount from inserted
  open tb_comexpproportionexpense_cur
  fetch next from tb_comexpproportionexpense_cur into @nbid,@nrelationbid, @ndcamount, @namount
  while( @@fetch_status = 0 )
  begin
    update tb_ComExpenditure set relationcount = relationcount+1, dcappamt=dcappamt+@ndcamount, appamt=appamt+@namount
    where bid = @nrelationbid     
    update tb_comexpproportion set amount=amount+@ndcamount where bid = @nbid
    fetch next from tb_comexpproportionexpense_cur into @nbid,@nrelationbid, @ndcamount, @namount
  end
  close tb_comexpproportionexpense_cur
  deallocate tb_comexpproportionexpense_cur
end


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

